﻿using System.Configuration;
using System.Runtime.CompilerServices;
using JiaBao.Core.Configuration;

namespace JiaBao.Core.Infrastructure
{
    /// <summary>
    /// 框架引擎上下文
    /// </summary>
    public class EngineContext
    {
        #region 属性
        public static IEngine Current
        {
            get
            {
                if (Singleton<IEngine>.Instance == null)
                    Initialize(false);
                return Singleton<IEngine>.Instance;
            }
        }

        #endregion

        /// <summary>
        ///  初始化框架引擎
        /// </summary>
        /// <param name="forceRecreate"></param>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.Synchronized)]//只能在一个线程上执行 保证唯一实例
        public static IEngine Initialize(bool forceRecreate)
        {
            if (Singleton<IEngine>.Instance == null || forceRecreate)
            {
                //创建新的引擎
                Singleton<IEngine>.Instance = new JiaBaoEngine();

                //获取配置文件
                //暂时不使用
               
                var config = new JiaBaoConfig();

               //初始化引擎
               Singleton<IEngine>.Instance.Initialize(config);

            }
            return Singleton<IEngine>.Instance;
        }

        /// <summary>
        /// 替换引擎
        /// </summary>
        /// <param name="engine"></param>
        public static void Replace(IEngine engine)
        {
            Singleton<IEngine>.Instance = engine;
        }

    }
}
